Contradictory Type Cast Or Check (CTCOC)

Description:

CTCOC detects cases where the same variable is cast to several incompatible types, or a variable is cast to some type and then checked to be an instance of an incompatible type. In this case, incompatible means that two target classes are not related, i.e., neither class is derived from the other.

Incorrect:

public char getLastChar(object obj) { 
   return ((System.Text.StringBuilder) obj)[((string) obj).Length - 1];
}

Correct:

public char getLastChar(object obj) { 
   return ((string) obj)[((string) obj).Length - 1];
}